﻿// The class that handles the creation of the application windows

using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;

namespace Daily
{
    public class ShowNotifyIcon : ApplicationContext
    {
        private NotifyIcon daily;
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem tsmiSetting;
        private ToolStripMenuItem tsmiShowMainForm;
        private ToolStripMenuItem tsmiExit;
        private ToolStripMenuItem tsmiAutoRun;
        private string _path;
        public ShowNotifyIcon()
        {
            _path = Assembly.GetExecutingAssembly().Location;
            try
            {
                InitializeComponent();
                StartAlert();
            }
            catch (IOException e)
            {
                // Inform the user that an error occurred.
                MessageBox.Show(e.ToString());

                // Exit the current thread instead of showing the windows.
                ExitThread();
            }
        }

        public void StartAlert()
        {
            System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
            t.Interval = 20000;
            t.Tick += new System.EventHandler(T_Tick);
            t.Start();
        }

        private void T_Tick(object sender, EventArgs e)
        {
            double hour = 0.0;
            double minute = 0.0;
            if (double.TryParse(ConfigManager.getConfigInfo("time_H"), out hour) && double.TryParse(ConfigManager.getConfigInfo("time_M"), out minute))
            {
                DateTime dt1 = DateTime.Now;
                if (dt1.Hour == hour && dt1.Minute == minute)
                {
                    daily.ShowBalloonTip(5000);
                }
            }
            else
            {
                daily.ShowBalloonTip(5000, "警告", "日报填写时间设置不正确！", ToolTipIcon.Warning);
            }
            if (double.TryParse(ConfigManager.getConfigInfo("time_H1"), out hour) && double.TryParse(ConfigManager.getConfigInfo("time_M1"), out minute))
            {
                DateTime dt1 = DateTime.Now;
                if (dt1.Hour == hour && dt1.Minute == minute)
                {
                    daily.ShowBalloonTip(5000);
                }
            }
            else
            {
                daily.ShowBalloonTip(5000, "警告", "日报填写时间设置不正确！", ToolTipIcon.Warning);
            }
        }

        private void InitializeComponent()
        {
            this.daily = new NotifyIcon();
            this.contextMenuStrip1 = new ContextMenuStrip();
            this.tsmiSetting = new ToolStripMenuItem();
            this.tsmiShowMainForm = new ToolStripMenuItem();
            this.tsmiExit = new ToolStripMenuItem();
            this.tsmiAutoRun = new ToolStripMenuItem();
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.tsmiSetting,tsmiAutoRun,
            this.tsmiShowMainForm,tsmiExit
        });
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(137, 48);
            // 
            // tsmiSetting
            // 
            this.tsmiSetting.Name = "tsmiSetting";
            this.tsmiSetting.Size = new System.Drawing.Size(136, 22);
            this.tsmiSetting.Text = "设置";
            this.tsmiSetting.MouseUp += new MouseEventHandler(TsmiSetting_MouseUp);
            // 
            // tsmiShowMainForm
            // 
            this.tsmiShowMainForm.Name = "tsmiShowMainForm";
            this.tsmiShowMainForm.Size = new System.Drawing.Size(136, 22);
            this.tsmiShowMainForm.Text = "显示主界面";
            //
            // tsmiExit
            //
            this.tsmiExit.Name = "tsmiExit";
            this.tsmiExit.Size = new System.Drawing.Size(136, 22);
            this.tsmiExit.Text = "退出";
            this.tsmiExit.MouseUp += new MouseEventHandler(TsmiExit_MouseUp);
            //
            // tsmiAutoRun
            //
            if (run.checkAutoRun(_path))
            {
                this.tsmiAutoRun.Image = new System.Drawing.Bitmap(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\image\\select.png");
            }
            else
            {
                this.tsmiAutoRun.Image = null;
            }
            this.tsmiAutoRun.Name = "tsmiAutoRun";
            this.tsmiAutoRun.Size = new System.Drawing.Size(136, 22);
            this.tsmiAutoRun.Text = "开机启动";
            this.tsmiAutoRun.MouseUp += new MouseEventHandler(tsmiAutoRun_MouseUp);
            // 
            // daily
            // 
            this.daily.BalloonTipText = "请填写日报";
            this.daily.BalloonTipTitle = "日报";
            this.daily.Icon = new System.Drawing.Icon(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\image\\logo.ico");
            this.daily.Text = "日报";
            this.daily.Visible = true;
            this.daily.ContextMenuStrip = contextMenuStrip1;

        }

        private void TsmiSetting_MouseUp(object sender, MouseEventArgs e)
        {
            Setting s = new Setting();
            s.ShowDialog();
        }

        private void tsmiAutoRun_MouseUp(object sender, MouseEventArgs e)
        {
            if (run.checkAutoRun(_path))
            {
                run.SetAutoRun(_path, false);
                this.tsmiAutoRun.Image = null;
            }
            else
            {
                run.SetAutoRun(_path, true);
                this.tsmiAutoRun.Image = new System.Drawing.Bitmap(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\image\\select.png");
            }
        }
        private void TsmiExit_MouseUp(object sender, MouseEventArgs e)
        {
            ExitThread();
        }
    }
}
